// Unicode 字符编码查询器 2个文本行分别显示字符和其Unicode 编码，2个按钮分别实现字符与Unicode编码的双向转换
package Example6point2;

import java.awt.*;
import java.awt.event.*;

public class QueryFrame extends Frame implements ActionListener {
    private TextField text_char, text_uni;
    private Button button_char, button_uni;

    public QueryFrame() {
        super("字符编码查询器");
        this.setBounds(300, 240, 1000, 200);
        this.setBackground(Color.lightGray);
        this.setLayout(new GridLayout(2, 3));
        this.add(new Label("字符", Label.RIGHT));
        this.text_char = new TextField("汉字", 10);
        this.add(this.text_char);
        this.button_char = new Button("查询Unicode码");
        this.add(this.button_char);
        this.button_char.addActionListener(this);
        this.add(new Label("Unicode 编码", Label.RIGHT));
        this.add(this.text_uni = new TextField(10));
        this.add(this.button_uni = new Button("查询字符"));
        this.button_uni.addActionListener(this);
        this.setVisible(true);
        this.pack();
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        if (event.getSource() == this.button_char) {
            String str = this.text_char.getText();
            if (!str.isEmpty()) {
                char ch = str.charAt(0);
                this.text_char.setText("" + ch);
                this.text_uni.setText("" + (int) ch);
            }
        } else if (event.getSource() == this.button_uni) {
            String str = this.text_uni.getText();
            if (!str.isEmpty())
                this.text_char.setText("" + (char) Integer.parseInt(str));
        }
    }

    public static void main(String[] args) {
        new QueryFrame();
    }
}
